package com.bdsy.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bdsy.system.dao.PermissionMapper;
import com.bdsy.system.model.Permission;
import com.bdsy.system.service.PermissionService;

/**
 * ClassName:UserServiceImpl
 * Description: TODO 添加描述.
 * @author   Administrator
 * @version  
 * @since    JDK 1.8
 */

@Service("permissionService")
public class PermissionServiceImpl implements PermissionService{

    @Autowired
    PermissionMapper permissionMapper;
	
	@Override
	public List<Permission> getAuthByid(String uid,String apid) {
		
		List<Permission> listpermissions = new ArrayList<Permission>();
		try {
			//取出一级菜单
			listpermissions = permissionMapper.selectAuthByid(uid, apid);
			if(listpermissions != null && listpermissions.size() > 0){
				for(Permission permission : listpermissions){
					String authId = permission.getAuthId();
					//取出二级菜单
					List<Permission> listper = permissionMapper.selectAuthByid(uid, authId);
					if(listper != null && listper.size() > 0){
						permission.setHasAuth(true);
						permission.setSubAuth(listper);
						if(permission.getAuthUrl() == null || "".equals(permission.getAuthUrl())){
							permission.setAuthUrl(null);
						}
						for(Permission findper : listper){
							String finAuthId = findper.getAuthId();
							//取出三级菜单
							List<Permission> list = permissionMapper.selectAuthByid(uid, finAuthId);
							if(list != null && list.size() > 0){
								findper.setHasAuth(true);
								findper.setSubAuth(list);
								if("".equals(findper.getAuthUrl()))
									findper.setAuthUrl(null);
							}
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return listpermissions;
	}
	
}

